public class Snake {
    // 食物信息
    private SnakeFood food;
    // 蛇头信息
    private SnakeHead head;
    // 蛇身信息
    private SnakeBody body;
    // 画布信息
    public static Canvas canvas;

    public Snake() {
        this(10, 60, '#');
    }

    public Snake(int row, int col, char ch) {
        this(new Canvas(row, col, ch));
    }

    public Snake(Canvas canvas) {
        this.canvas = canvas;
        head = new SnakeHead(canvas);
        body = new SnakeBody(3, head);
        food = new SnakeFood(body);
    }

    public Snake(Canvas canvas, SnakeHead head, SnakeBody body, SnakeFood food) {
        this.canvas = canvas;
        this.head = head;
        this.body = body;
        this.food = food;
    }

    public Snake setHead(int x, int y, int color, char ch, char dirent) {
        head.set(x, y, color, ch, dirent);
        return this;
    }

    public Snake setBody(int num, int color, char ch) {
        body.set(num, color, ch);
        return this;
    }

    public Snake setFood(int color, char ch) {
        food.set(color, ch, body);
        return this;
    }

    public Snake setDir(char dirent) {
        head.setDir(dirent);
        return this;
    }

    public Snake draw() {
        canvas.init();
        canvas.set(head);
        canvas.set(food);
        body.draw();
        return this;
    }

    public int getColor(char ch) {
        if (ch == head.ch)
            return head.color;
        else if (ch == food.ch)
            return food.color;
        else if (ch == body.ch)
            return body.color;
        return 0;
    }

    public boolean move() {
        if (!canvas.check(head) || body.check())
            return true;

        if (head.eq(food)) {
            body.grow(food);
            food.rand();
        }

        body.move();
        return false;
    }
}
